package com.xxzx.publicprevention.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xxzx.publicprevention.pojo.*;
import com.xxzx.publicprevention.service.RecordService;
import com.xxzx.publicprevention.service.RecordfilerelationService;
import com.xxzx.publicprevention.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liyu
 * @since 2020-11-04
 */
@CrossOrigin
@Controller
@RequestMapping("/recordDO")
public class RecordController {
    @Autowired
    private RecordService recordService;
    @Autowired
    private RecordfilerelationService recordfilerelationService;

    @Autowired
    private UserService userService;

    @Autowired
    private TbUploadfileController uploadfileController;


    @RequestMapping(value = "/getAllRecord", method = RequestMethod.GET)
    @ResponseBody
    public List<RecordDO> getAllRecord() {

        List<RecordDO> list = new ArrayList<>();


        try {
            list = recordService.getAllRecord();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return list;

    }

    @RequestMapping(value = "/getRecordByType", method = RequestMethod.GET)
    @ResponseBody
    public List<RecordDO> getRecordByType(int type) {
        List<RecordDO> list = new ArrayList<>();
        try {
            list = recordService.getRecordByType(type);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return list;
    }

    @RequestMapping(value = "/getRecords", method = RequestMethod.GET)
    @ResponseBody
    public ResultMap<List<RecordDO>> getRecords(@RequestParam("page") Integer curr, @RequestParam("limit") Integer size) {

        List<RecordDO> list = new ArrayList<>();
        ResultMap<List<RecordDO>> resultMap = new ResultMap<>();
        try {
            int total = recordService.getCount();
            list = recordService.getRecordByPage(curr - 1, size);
            resultMap = new ResultMap<>(0, "", total, list);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return resultMap;

    }

    @RequestMapping(value = "/getRecordsByType", method = RequestMethod.GET)
    @ResponseBody
    public ResultMap<List<RecordDO>> getRecordsByType(int type, @RequestParam("page") Integer curr, @RequestParam("limit") Integer size) {
        List<RecordDO> list = new ArrayList<>();
        ResultMap<List<RecordDO>> resultMap = new ResultMap<>();
        try {
            List<RecordDO> list1 = recordService.getRecordByType(type);
            list = recordService.getRecordByPageAndType(curr - 1, size, type);
            resultMap = new ResultMap<>(0, "", list1.size(), list);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return resultMap;

    }

    @RequestMapping(value = "/getRecordByUserId", method = RequestMethod.GET)
    @ResponseBody
    public List<RecordDO> getRecordByUserId(Integer id, Integer beginNum, Integer rowCount, String timeBegin, String timeEnd) {
        List<RecordDO> list = new ArrayList<>();
        try {
            list = recordService.getRecordByUserId(id, beginNum, rowCount, timeBegin, timeEnd);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return list;

    }

    @RequestMapping(value = "/getRecordByUserIdAndDetail", method = RequestMethod.GET)
    @ResponseBody
    public List<RecordDO> getRecordByUserIdAndDetail(Integer id, Integer beginNum, Integer rowCount, String detail) {
        List<RecordDO> list = new ArrayList<>();
        try {
            list = recordService.getRecordByUserIdAndDetail(id, beginNum, rowCount, detail);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return list;

    }


    @RequestMapping(value = "/getFilesByRecordId", method = RequestMethod.GET)
    @ResponseBody
    public List<TbUploadfile> getFilesByRecordId(int id) {
        List<TbUploadfile> list = new ArrayList<>();
        try {
            List<RecordfilerelationDO> relations = recordfilerelationService.getRelationsByRecordId(id);
            if (relations.size() > 0) {
                for (RecordfilerelationDO c : relations) {
                    TbUploadfile file = uploadfileController.selectOne(c.getFileid());
                    list.add(file);
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return list;

    }


    @RequestMapping(value = "/getRecordByOpenid", method = RequestMethod.GET)
    @ResponseBody
    public List<RecordDO> getRecordByOpenid(String openId, Integer beginNum, Integer rowCount, String timeBegin, String timeEnd) {
        try {
            UserDO userDO = userService.getUserByOpenId(openId);
            List<RecordDO> records = getRecordByUserId(userDO.getId(), beginNum, rowCount, timeBegin, timeEnd);
            return records;
        } catch (Exception e) {
            return null;
        }
    }

    @RequestMapping(value = "/getRecordByOpenidAndDetail", method = RequestMethod.GET)
    @ResponseBody
    public List<RecordDO> getRecordByOpenidAndDetail(String openId, Integer beginNum, Integer rowCount, String detail) {
        try {
            UserDO userDO = userService.getUserByOpenId(openId);
            List<RecordDO> records = getRecordByUserIdAndDetail(userDO.getId(), beginNum, rowCount, detail);
            return records;
        } catch (Exception e) {
            return null;
        }
    }




    @PreAuthorize("hasAuthority('群测群防点管理权限')")
    @RequestMapping(value = "/saveRecord", method = RequestMethod.POST)
    @ResponseBody
    public Boolean saveRecord(String record, String attaIds) {
        boolean flag = false;


        try {
            RecordDO recordDO = JSON.parseObject(record, RecordDO.class);

            Integer id = recordService.saveRecord(recordDO);
            // 关联附件
            List<Integer> aIds = JSONArray.parseArray(attaIds, Integer.class);

            if (aIds != null) {
                RecordfilerelationDO rela;
                for (Integer aid : aIds) {
                    rela = new RecordfilerelationDO();
                    rela.setRecordid(id);
                    rela.setFileid(aid);
                    recordfilerelationService.saveRelations(rela);
                }
            }
            flag = true;


        } catch (Exception e) {

            e.printStackTrace();
        }

        return flag;
    }

    @PreAuthorize("hasAuthority('群测群防点管理权限')")
    @RequestMapping(value = "/editRecord", method = RequestMethod.POST)
    @ResponseBody
    public Boolean editRecord(String record) {
        boolean flag = false;
        try {
            RecordDO recordDO = JSON.parseObject(record, RecordDO.class);
            recordService.editRecord(recordDO);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


    @RequestMapping(value = "/getRecordById", method = RequestMethod.GET)
    @ResponseBody
    public RecordDO getRecordById(Integer id) {
        RecordDO recordDO = recordService.getRecordById(id);
        return recordDO;
    }

}

